package com.kiss.rpc.utils.cal_time;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MethodTimeProxyHandler implements InvocationHandler {

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Long startTime = System.currentTimeMillis();
        method.invoke(proxy, args);
        Long endTime = System.currentTimeMillis();
        System.out.printf("【%s】方法使用了%ds", method.getName(), endTime - startTime);
        return proxy;
    }

    public static Object getProxy(Object obj){
        ClassLoader classLoader = obj.getClass().getClassLoader();
        Class<?>[] classes = new Class[]{obj.getClass()};
        MethodTimeProxyHandler proxyHandler = new MethodTimeProxyHandler();

        return Proxy.newProxyInstance(classLoader, classes, proxyHandler);
    }
}
